package com.googlecode.sc2dm.server.authentication;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;

/**
 * @Author Jarle Hansen (jarle@jarlehansen.net)
 * Created: 8:29 PM - 1/7/11
 */
public class GoogleAuthentication implements Authentication {
    private final Logger logger = LoggerFactory.getLogger(GoogleAuthentication.class);

    private final AuthenticationData authenticationData;
    private AuthenticationDao authenticationDao;

    public GoogleAuthentication(AuthenticationData authenticationData) {
        this.authenticationData = authenticationData;
        this.authenticationDao = new DefaultAuthenticationDao();
    }

    public void setAuthenticationDao(AuthenticationDao authenticationDao) {
        this.authenticationDao = authenticationDao;
    }

    public String getAuthToken() {
        String token = authenticationDao.getPersistedToken();

        if (token == null) {
            try {
                HttpURLConnection connection = authenticationData.getHttpConnection();
                connection.setDoOutput(true);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");

                OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
                writer.write(authenticationData.createAuthString());
                writer.close();

                String response = null;
                //Get the response
                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {

                    BufferedReader rd = new BufferedReader(new
                            InputStreamReader(connection.getInputStream()));
                    String line;

                    while ((line = rd.readLine()) != null) {
                        response += line;
                    }

                    rd.close();
                }

                if (response == null) {
                    logger.warn("Unable to retrieve token, http response is null");
                } else {
                    String[] parts = response.split("=");
                    token = parts[parts.length - 1];

                    logger.info("Token received: " + token);
                }
            } catch (IOException io) {
                logger.error("Unable to connect to the c2dm authentication url", io);
                throw new IllegalStateException("Unable to connect to the c2dm authentication url");
            }
        }

        return token;
    }
}
